//
//  XiAndShouViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "XiAndShouViewController.h"

@interface XiAndShouViewController ()<UIWebViewDelegate,UINavigationBarDelegate>

@property (nonatomic, strong) UIBarButtonItem *backBT;

@property (nonatomic, strong) DDIndicator *indicator;

@property (nonatomic, strong) UIView *viewV;


@property (nonatomic, strong) AppDelegate *app;

@end

@implementation XiAndShouViewController

//- (void)viewWillAppear:(BOOL)animated
//{
//    self.tabBarController.tabBar.hidden = YES;
//}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"reloadData" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _app = [[AppDelegate alloc] init];
    [self setNaviBar];
    [self requestData];
    [self addInternetObserver];
}

// 通知网络状态
- (void)addInternetObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataWithInternet:) name:@"reloadData" object:nil];
}

- (void)reloadDataWithInternet:(NSNotification *)notification
{
    [self requestData];
}



- (void)requestData
{
    if ([_app internetReachableOrNot]) {
        if (self.detailsUrl != nil) {
            [self addAnimation];
            //  创建webView
            UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            //  设置代理
            webView.delegate = self;
            webView.scalesPageToFit = YES;
            //  请求网址
            NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.detailsUrl]];
            //  加载网页
            [webView loadRequest:request];
            //  添加视图
            [self.view addSubview:webView];
        } else {
            UIImageView *imagV = [[UIImageView alloc] initWithFrame:self.view.bounds];
            imagV.image = [UIImage imageNamed:@"meiyou_background.jpg"];
            [self.view addSubview:imagV];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kScreenHeight / 2 - 100, kScreenWidth, 30)];
            label.text = @"❌ 抱歉! 暂无相关数据!";
            label.textColor = [UIColor redColor];
            label.textAlignment = YES;
            [imagV addSubview:label];
        }
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"🙅 您的当前网络中断或网络信号差, 请检查网络后重新进入!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
    }
}

- (void)setNaviBar
{
    self.navigationItem.title = self.cityName;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backNew.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionLeftButton:)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}

- (void)actionLeftButton:(UIBarButtonItem *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}




// 加载动画
- (void)addAnimation
{
    _viewV = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth / 2 - 100, kScreenHeight / 2 - 80, 200, 80)];
    _viewV.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    _viewV.layer.cornerRadius = 10;
    _viewV.layer.masksToBounds = YES;
    [self.view addSubview:_viewV];
    
    self.indicator = [[DDIndicator alloc] initWithFrame:CGRectMake(_viewV.width / 2 - 20, _viewV.height / 2 - 20 , 40, 40)];
    [_viewV addSubview:self.indicator];
    [self.indicator startAnimating];
   // [self performSelector:@selector(stopLoading:) withObject:self.indicator afterDelay:5];
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self stopLoading];

}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
}

- (void)stopLoading
{
    [_indicator stopAnimating];
    [self.viewV removeFromSuperview];
}



#pragma mark -- 返回按钮
- (void)actionBack:(UIBarButtonItem *)button
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
